import { ElMessageBox } from 'element-plus';

// 截取截取路由字符串
export const interceptStr = (str: string): string => {
  const strArr = str.split('_');
  const routeName = str.replace(`_${strArr[strArr.length - 1]}`, '');
  return routeName;
};

// 处理提示框
export const handleMessageBox = (str: string, title: string) => {
  return new Promise((resolve, resject) => {
    ElMessageBox.confirm(str, title, { confirmButtonText: '确定', cancelButtonText: '取消' })
      .then(() => {
        resolve(true);
      })
      .catch(() => {
        resject();
      });
  });
};

// 处理文件下载
export function exportFileUtil(res: any) {
  const fileName = res.headers.name || `列表.xlsx`;
  const blob = new Blob([res.data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
  });
  const url = window.URL.createObjectURL(blob);
  const aLink = document.createElement('a');
  aLink.style.display = 'none';
  aLink.href = url;
  aLink.setAttribute('download', decodeURI(fileName));
  document.body.appendChild(aLink);
  aLink.click();
  document.body.removeChild(aLink); // 下载完成移除元素
  window.URL.revokeObjectURL(url); // 释放掉blob对象
}

// 车辆来源类型
export const filterName = (type: number) => {
  switch (type) {
    case 1:
      return '库融车';
    case 2:
      return '返利车';
    default:
      return '现金车';
  }
};

// 格式化千分位
export function splitThousandSeparator(num: any) {
  if (num) {
    num = typeof num === 'string' ? parseFloat(num) : num; // 判断是否是字符串如果是字符串转成数字
    num = num.toFixed(2); // 保留两位
    num = parseFloat(num); // 转成数字
    num = num.toLocaleString(); // 转成金额显示模式
    // 判断是否有小数
    if (num.indexOf('.') === -1) {
      num += '.00';
    } else {
      num = num.split('.')[1].length < 2 ? `${num}0` : num;
    }
    return num; // 返回的是具有千分位格式并保留2位小数的字符串
  }
  return '--';
}
